package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.utils.model.ListMapModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.base.config.PageData;
import com.example.base.system.service.SysTableService;
import com.example.base.system.domain.SysTable;
import java.io.Serializable;
import java.util.Map;

import com.example.base.system.domain.vo.SysTableVo;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2024-02-21
 */

@RestController
@Api(tags = {"的服务类"})
@RequestMapping("/sys-table")
public class SysTableController  {

        @Autowired
        private SysTableService sysTableService;


        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysTable-saveorupdate",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的新增/更新接口",notes = "的新增接口")
        public JSONObject save(@RequestBody @Validated SysTableVo sysTableVo) throws GlobalExcetion{
                sysTableService.saveOrUpdateSysTable(sysTableVo);
                return Result.successInfo("操作成功");
        }



        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysTable-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysTableService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysTable-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysTableService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysTable-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysTable> list= sysTableService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/getbyid")
        @IPRateLimit(name = "SysTable-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
                SysTable data= sysTableService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysTable-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                IPage<SysTable> data=sysTableService.page(page, pageSize);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }

        @GetMapping("gettables")
        public JSONObject getTables(){
                List<Map<String,String>> mapList=sysTableService.systemTableList();
                for (int i = 0; i < mapList.size() ; i++) {
                        Map<String,String> map=mapList.get(i);
                        String name=map.get("table_comment")==null ? "无名表" : map.get("table_comment");
                        String tableName=map.get("table_name");
                        map.put("name",name+"/"+tableName);
                }
                return Result.successInfo(mapList);
        }

        @GetMapping("gettableinfo")
        public JSONObject getTableInfo(@Param("tableName")String tableName){
                List<Map<String,Object>> mapList=sysTableService.getTableInfo(tableName);
                if(mapList!=null && !mapList.isEmpty()){
                        ListMapModel listMapModel=new ListMapModel(mapList);
                        listMapModel.replace("COLUMN_COMMENT","tableColumnName");
                        listMapModel.replace("COLUMN_NAME","tableColumn");
                        listMapModel.replace("CHARACTER_MAXIMUM_LENGTH","tableColumnLength",0);
                        listMapModel.addDefaultValue("filters", new ArrayList<>());
                        listMapModel.addDefaultValue("tableHide",false);
                        listMapModel.addDefaultValue("sortable",false);
                        listMapModel.remove("IS_NULLABLE","DATA_TYPE","COLUMN_KEY");
                        listMapModel.execute();
                }
                return Result.successInfo(mapList);
        }


}

